import { _decorator, Component, EventTouch, Input, instantiate, Node, Prefab, RenderTexture, SpriteFrame, UITransform, Vec2, Vec3 } from 'cc';
import { preBlockClick } from './preBlockClick';
import { gameState } from './gameState';
import { layerRootAction } from './layerRootAction';
const { ccclass, property } = _decorator;

@ccclass('layerAction_001')
export class layerAction_001 extends Component {
    @property(Prefab) pre_block:Prefab = null;
    @property([SpriteFrame]) types_array:SpriteFrame[] = [];
    cur_block_action:preBlockClick = null; 
    add_block_by_world_pos(world:Vec3):preBlockClick{
        
        let local = this.node.getComponent(UITransform).convertToNodeSpaceAR(world);
        return this.add_block_by_local_pos(local);
    }
    add_block_by_local_pos(local:Vec3):preBlockClick{
        let block = instantiate(this.pre_block);
        block.setPosition(local);
        block.setParent(this.node);
        return block.getComponent(preBlockClick);
    }
    subtract_block(world:Vec3){
        let local = this.node.getComponent(UITransform).convertToNodeSpaceAR(world);
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            if (this.node.children[i].getComponent(UITransform).getBoundingBox().contains(new Vec2(local.x, local.y))) {
                this.node.children[i].removeFromParent();
                break;
            }
        }
    }
    clear_all(){
        this.node.removeAllChildren();
    }
    refresh_shadow(){
        for(let i = 0;i<this.node.children.length;i++){
            let elem_1 = this.node.children[i].getComponent(preBlockClick);
            if(!elem_1.node.active)continue;
            elem_1.show();
            for(let j = i+1;j<this.node.children.length;j++){
                let elem_2 = this.node.children[j].getComponent(preBlockClick);
                if(!elem_2.node.active)continue;
                if(elem_1.get_custom_bounding_box().intersects(elem_2.get_custom_bounding_box())){
                    elem_1.hide();
                    break;
                }
            }
        }
    }
    get_children():Node[]{
        return this.node.children;
    }
    start_game(lvl:number){
        let lvl_data = gameState.get_data_by_lvl(lvl);
        let types_random_index_arr = this.get_random_index_array(lvl_data.types);
        let current_index = 0;
        for(let index = 0;index<lvl_data.list.length;index++){
            let ele = lvl_data.list[index];
            if(index>0&&index%3==0){
                current_index++;
                if(current_index>=types_random_index_arr.length){
                    current_index = 0;
                }
            }
            let pre_block_action = this.add_block_by_local_pos(new Vec3(ele.x,ele.y));
            pre_block_action.init(types_random_index_arr[current_index],this.types_array);
            pre_block_action.refresh_Sprite(true);
        }
        this.refresh_shadow();
    }
    get_random_index_array(types:number):number[]{
        let arr:number[] = [];
        for (let i = 0; i < this.types_array.length; i++) {
            arr.push(i);
            
        }

        arr.sort(()=>{
            return 0.5-Math.random();
        })
        arr.splice(0,arr.length-types);
        console.info(arr);
        return arr;
    }
    random_block(){
        let temp:preBlockClick[] = [];
        this.node.children.forEach(ele => {
            let action = ele.getComponent(preBlockClick);
            if(action.node.active)temp.push(action);
        });
        temp.forEach(elem => {
            let other = temp[Math.floor(Math.random()*temp.length)];
            let other_index = other.get_sprite_index();
            other.set_sprite_index(elem.get_sprite_index());
            elem.set_sprite_index(other_index);
            other.refresh_Sprite(true);
            elem.refresh_Sprite(true);
        });
    }
    start() {
        if(gameState.game_model ===1){
            return;
        }
        this.node.on(Input.EventType.TOUCH_END,this.touch_end,this)
        this.node.on(Input.EventType.TOUCH_START,this.touch_start,this)
        this.node.on(Input.EventType.TOUCH_CANCEL,this.touch_cancel,this)
    }
    touch_end(e:EventTouch){
        if(this.node.parent.getComponent(layerRootAction).get_layer_3_size()>=7)return;
        let pos = e.getUILocation();//getLocation

        if(this.cur_block_action){
            this.cur_block_action.play_end_tween();
        }else{
            return;
        }
        let block_action = this.get_touch_block(pos);
        if(this.cur_block_action!=block_action){
            return;
        }
        this.node.parent.getComponent(layerRootAction).play_sound(0);
        this.node.parent.getComponent(layerRootAction).to_3_from_1(this.cur_block_action,this.pre_block);
    }   
    touch_start(e:EventTouch){
        let pos = e.getUILocation();//getLocation
        console.info(pos);
        let block_action = this.get_touch_block(pos);
        this.cur_block_action = block_action;
        if(this.cur_block_action){
            this.cur_block_action.play_start_tween();
        }
        
    }
    touch_cancel(e:EventTouch){
        if(this.cur_block_action){
            this.cur_block_action.play_end_tween();
        }
    }
    get_touch_block(pos_world:Vec2):preBlockClick{
        let local = this.node.getComponent(UITransform).convertToNodeSpaceAR(new Vec3(pos_world.x,pos_world.y));
        console.info(local);
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            let ele = this.node.children[i];
            if (!ele.active) {
                continue;
            }
            let block_action = ele.getComponent(preBlockClick);
            if(!block_action.can_touch()){
                continue;
            }
            if(block_action.get_bounding_box().contains(new Vec2(local.x,local.y))){
                return block_action;
            }
        }
        return null;
    }
    get_block_size():number{
        return this.node.children.length;
    }
    update(deltaTime: number) {
        
    }
}


